using AForge.Imaging;
using AForge.Video;
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Threading;
using System.Timers;
using System.Windows.Forms;
namespace SilverSea.Video.Tools
{
	public class VideoSourcePlayer : Control, IVideoSourcePlayer
	{
		private IContainer icontainer_0;
		private IVideoSource ivideoSource_0;
		private System.Drawing.Bitmap bitmap_0;
		private string string_0;
		private string string_1 = string.Empty;
		private TextOverlayLocation textOverlayLocation_0 = TextOverlayLocation.BottomLeft;
		private System.Drawing.Color color_0 = System.Drawing.Color.Blue;
		private System.Drawing.Color color_1 = System.Drawing.Color.Black;
		private System.Drawing.Color color_2 = System.Drawing.Color.Green;
		private int int_0 = 1;
		private System.Drawing.Rectangle[] rectangle_0;
		private bool bool_0;
		private bool bool_1;
		private bool bool_2 = true;
		private volatile bool bool_3;
		private VideoSourceErrorEventHandler videoSourceErrorEventHandler_0;
		private VideoWriterFinishEventHandler videoWriterFinishEventHandler_0;
		private DateTime dateTime_0 = DateTime.Now;
		private string string_2 = string.Empty;
		private AVIWriter aviwriter_0;
		private System.Timers.Timer timer_0 = new System.Timers.Timer();
		private BackgroundWorker backgroundWorker_0;
		private Queue queue_0 = new Queue();
		private System.Timers.Timer timer_1 = new System.Timers.Timer();
		private int int_1;
		private System.Timers.Timer timer_2 = new System.Timers.Timer();
		private object object_0 = new object();
		private Control control_0;
		private float float_0;
		private float float_1;
		private bool bool_4;
		private int int_2 = 5;
		private string string_3 = "div4";
		private int int_3 = 30;
		public string videoFolder = string.Empty;
		private NewFrameHandler newFrameHandler_0;
		private int int_4;
		private int int_5;
		private int[] int_6 = new int[15];
		private long[] long_0 = new long[15];
		public event VideoSourceErrorEventHandler VideoSourceError
		{
			add
			{
				VideoSourceErrorEventHandler videoSourceErrorEventHandler = this.videoSourceErrorEventHandler_0;
				VideoSourceErrorEventHandler videoSourceErrorEventHandler2;
				do
				{
					videoSourceErrorEventHandler2 = videoSourceErrorEventHandler;
					VideoSourceErrorEventHandler value2 = (VideoSourceErrorEventHandler)Delegate.Combine(videoSourceErrorEventHandler2, value);
					videoSourceErrorEventHandler = Interlocked.CompareExchange<VideoSourceErrorEventHandler>(ref this.videoSourceErrorEventHandler_0, value2, videoSourceErrorEventHandler2);
				}
				while (videoSourceErrorEventHandler != videoSourceErrorEventHandler2);
			}
			remove
			{
				VideoSourceErrorEventHandler videoSourceErrorEventHandler = this.videoSourceErrorEventHandler_0;
				VideoSourceErrorEventHandler videoSourceErrorEventHandler2;
				do
				{
					videoSourceErrorEventHandler2 = videoSourceErrorEventHandler;
					VideoSourceErrorEventHandler value2 = (VideoSourceErrorEventHandler)Delegate.Remove(videoSourceErrorEventHandler2, value);
					videoSourceErrorEventHandler = Interlocked.CompareExchange<VideoSourceErrorEventHandler>(ref this.videoSourceErrorEventHandler_0, value2, videoSourceErrorEventHandler2);
				}
				while (videoSourceErrorEventHandler != videoSourceErrorEventHandler2);
			}
		}
		public event VideoWriterFinishEventHandler VideoWriterFinish
		{
			add
			{
				VideoWriterFinishEventHandler videoWriterFinishEventHandler = this.videoWriterFinishEventHandler_0;
				VideoWriterFinishEventHandler videoWriterFinishEventHandler2;
				do
				{
					videoWriterFinishEventHandler2 = videoWriterFinishEventHandler;
					VideoWriterFinishEventHandler value2 = (VideoWriterFinishEventHandler)Delegate.Combine(videoWriterFinishEventHandler2, value);
					videoWriterFinishEventHandler = Interlocked.CompareExchange<VideoWriterFinishEventHandler>(ref this.videoWriterFinishEventHandler_0, value2, videoWriterFinishEventHandler2);
				}
				while (videoWriterFinishEventHandler != videoWriterFinishEventHandler2);
			}
			remove
			{
				VideoWriterFinishEventHandler videoWriterFinishEventHandler = this.videoWriterFinishEventHandler_0;
				VideoWriterFinishEventHandler videoWriterFinishEventHandler2;
				do
				{
					videoWriterFinishEventHandler2 = videoWriterFinishEventHandler;
					VideoWriterFinishEventHandler value2 = (VideoWriterFinishEventHandler)Delegate.Remove(videoWriterFinishEventHandler2, value);
					videoWriterFinishEventHandler = Interlocked.CompareExchange<VideoWriterFinishEventHandler>(ref this.videoWriterFinishEventHandler_0, value2, videoWriterFinishEventHandler2);
				}
				while (videoWriterFinishEventHandler != videoWriterFinishEventHandler2);
			}
		}
		public event NewFrameHandler NewFrame
		{
			add
			{
				NewFrameHandler newFrameHandler = this.newFrameHandler_0;
				NewFrameHandler newFrameHandler2;
				do
				{
					newFrameHandler2 = newFrameHandler;
					NewFrameHandler value2 = (NewFrameHandler)Delegate.Combine(newFrameHandler2, value);
					newFrameHandler = Interlocked.CompareExchange<NewFrameHandler>(ref this.newFrameHandler_0, value2, newFrameHandler2);
				}
				while (newFrameHandler != newFrameHandler2);
			}
			remove
			{
				NewFrameHandler newFrameHandler = this.newFrameHandler_0;
				NewFrameHandler newFrameHandler2;
				do
				{
					newFrameHandler2 = newFrameHandler;
					NewFrameHandler value2 = (NewFrameHandler)Delegate.Remove(newFrameHandler2, value);
					newFrameHandler = Interlocked.CompareExchange<NewFrameHandler>(ref this.newFrameHandler_0, value2, newFrameHandler2);
				}
				while (newFrameHandler != newFrameHandler2);
			}
		}
		[DefaultValue(false)]
		public bool AutoSizeControl
		{
			get
			{
				return this.bool_0;
			}
			set
			{
				this.bool_0 = value;
				this.method_2();
			}
		}
		[DefaultValue(typeof(System.Drawing.Color), "Black")]
		public System.Drawing.Color BorderColor
		{
			get
			{
				return this.color_1;
			}
			set
			{
				this.color_1 = value;
				base.Invalidate();
			}
		}
		[DefaultValue(typeof(System.Drawing.Color), "Green")]
		public System.Drawing.Color MotionRectangleColor
		{
			get
			{
				return this.color_2;
			}
			set
			{
				this.color_2 = value;
				base.Invalidate();
			}
		}
		[DefaultValue(1)]
		public int MotionRectangleLineWidth
		{
			get
			{
				return this.int_0;
			}
			set
			{
				this.int_0 = value;
				base.Invalidate();
			}
		}
		public System.Drawing.Rectangle[] MotionRectangles
		{
			get
			{
				return this.rectangle_0;
			}
			set
			{
				this.rectangle_0 = value;
			}
		}
		public string TextOverlay
		{
			get
			{
				return this.string_1;
			}
			set
			{
				this.string_1 = value;
			}
		}
		public TextOverlayLocation TextOverlayLocation
		{
			get
			{
				return this.textOverlayLocation_0;
			}
			set
			{
				this.textOverlayLocation_0 = value;
			}
		}
		public System.Drawing.Color TextOverlayColor
		{
			get
			{
				return this.color_0;
			}
			set
			{
				this.color_0 = value;
			}
		}
		public float FPS
		{
			get
			{
				return this.float_0;
			}
		}
		public float BPS
		{
			get
			{
				return this.float_1;
			}
		}
		public bool EnableRecording
		{
			get
			{
				return this.bool_4;
			}
			set
			{
				this.bool_4 = value;
			}
		}
		public int RecordingRate
		{
			get
			{
				return this.int_2;
			}
			set
			{
				this.int_2 = value;
			}
		}
		public string RecordingCodec
		{
			get
			{
				return this.string_3;
			}
			set
			{
				this.string_3 = value;
			}
		}
		public int VideoLengthLimit
		{
			get
			{
				return this.int_3;
			}
			set
			{
				this.int_3 = value;
			}
		}
		public string VideoFolder
		{
			get
			{
				return this.videoFolder;
			}
			set
			{
				this.videoFolder = value;
			}
		}
		[Browsable(false)]
		public IVideoSource VideoSource
		{
			get
			{
				return this.ivideoSource_0;
			}
			set
			{
				lock (this.object_0)
				{
					if (this.ivideoSource_0 != null)
					{
						this.ivideoSource_0.NewFrame -= new NewFrameEventHandler(this.ivideoSource_0_NewFrame);
						this.ivideoSource_0.VideoSourceError -= new VideoSourceErrorEventHandler(this.ivideoSource_0_VideoSourceError);
						this.ivideoSource_0.PlayingFinished -= new PlayingFinishedEventHandler(this.ivideoSource_0_PlayingFinished);
					}
					if (this.bitmap_0 != null)
					{
						this.bitmap_0.Dispose();
						this.bitmap_0 = null;
					}
					this.ivideoSource_0 = value;
					if (this.ivideoSource_0 != null)
					{
						this.ivideoSource_0.NewFrame += new NewFrameEventHandler(this.ivideoSource_0_NewFrame);
						this.ivideoSource_0.VideoSourceError += new VideoSourceErrorEventHandler(this.ivideoSource_0_VideoSourceError);
						this.ivideoSource_0.PlayingFinished += new PlayingFinishedEventHandler(this.ivideoSource_0_PlayingFinished);
					}
					this.string_0 = null;
					this.bool_1 = true;
					this.bool_2 = true;
					this.int_5 = 0;
					this.int_4 = 0;
					base.Invalidate();
				}
			}
		}
		[Browsable(false)]
		public bool IsRunning
		{
			get
			{
				bool result;
				lock (this.object_0)
				{
					result = (this.ivideoSource_0 != null && this.ivideoSource_0.IsRunning);
				}
				return result;
			}
		}
		public DateTime LastTimeReceivedFrame
		{
			get
			{
				return this.dateTime_0;
			}
			set
			{
				this.dateTime_0 = value;
			}
		}
		public VideoSourcePlayer()
		{
			this.method_0();
			base.SetStyle(ControlStyles.UserPaint | ControlStyles.ResizeRedraw | ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer, true);
			this.timer_2.Interval = 1000.0;
			this.timer_2.Elapsed += new ElapsedEventHandler(this.timer_2_Elapsed);
		}
		protected override void Dispose(bool disposing)
		{
			if (disposing && this.icontainer_0 != null)
			{
				this.icontainer_0.Dispose();
			}
			base.Dispose(disposing);
		}
		private void method_0()
		{
			base.SuspendLayout();
			base.Paint += new PaintEventHandler(this.VideoSourcePlayer_Paint);
			base.ParentChanged += new EventHandler(this.VideoSourcePlayer_ParentChanged);
			base.ResumeLayout(false);
		}
		private void method_1()
		{
			if (!base.IsHandleCreated)
			{
				base.CreateControl();
				if (!base.IsHandleCreated)
				{
					this.CreateHandle();
				}
			}
			if (base.InvokeRequired)
			{
				throw new InvalidOperationException("Cross thread access to the control is not allowed.");
			}
		}
		public void Start()
		{
			this.bool_3 = false;
			lock (this.object_0)
			{
				if (this.ivideoSource_0 != null)
				{
					this.bool_2 = true;
					this.int_5 = 0;
					this.int_4 = 0;
					this.ivideoSource_0.Start();
					this.timer_2.Start();
					base.Invalidate();
				}
			}
		}
		public void Stop()
		{
			this.bool_3 = true;
			lock (this.object_0)
			{
				if (this.ivideoSource_0 != null)
				{
					this.timer_2.Stop();
					this.ivideoSource_0.Stop();
					if (this.bitmap_0 != null)
					{
						this.bitmap_0.Dispose();
						this.bitmap_0 = null;
					}
					base.Invalidate();
				}
			}
		}
		public void SignalToStop()
		{
			this.bool_3 = true;
			lock (this.object_0)
			{
				if (this.ivideoSource_0 != null)
				{
					this.ivideoSource_0.SignalToStop();
				}
			}
		}
		public void WaitForStop()
		{
			this.bool_3 = true;
			lock (this.object_0)
			{
				if (this.ivideoSource_0 != null)
				{
					this.ivideoSource_0.WaitForStop();
					if (this.bitmap_0 != null)
					{
						this.bitmap_0.Dispose();
						this.bitmap_0 = null;
					}
					base.Invalidate();
				}
			}
		}
		public void SaveCurrentVideoFrame(string path)
		{
			lock (this.object_0)
			{
				if (this.bitmap_0 != null)
				{
					this.bitmap_0.Save(path, System.Drawing.Imaging.ImageFormat.Jpeg);
				}
			}
		}
		public System.Drawing.Bitmap GetCurrentVideoFrame()
		{
			System.Drawing.Bitmap result;
			lock (this.object_0)
			{
				result = ((this.bitmap_0 == null) ? null : ((System.Drawing.Bitmap)this.bitmap_0.Clone()));
			}
			return result;
		}
		public UnmanagedImage GetCurrentUnmanagedVideoFrame()
		{
			UnmanagedImage result;
			lock (this.object_0)
			{
				result = ((this.bitmap_0 == null) ? null : UnmanagedImage.FromManagedImage((System.Drawing.Bitmap)this.bitmap_0.Clone()));
			}
			return result;
		}
		private void VideoSourcePlayer_Paint(object sender, PaintEventArgs e)
		{
			try
			{
				if (this.bool_1 || this.bool_2)
				{
					this.method_2();
					this.bool_1 = false;
				}
				lock (this.object_0)
				{
					System.Drawing.Graphics graphics = e.Graphics;
					System.Drawing.Rectangle clientRectangle = base.ClientRectangle;
					System.Drawing.Pen pen = new System.Drawing.Pen(this.color_1, 1f);
					graphics.DrawRectangle(pen, clientRectangle.X, clientRectangle.Y, clientRectangle.Width - 1, clientRectangle.Height - 1);
					if (this.ivideoSource_0 != null)
					{
						if (this.bitmap_0 != null && this.string_0 == null)
						{
							graphics.DrawImage(this.bitmap_0, clientRectangle.X + 1, clientRectangle.Y + 1, clientRectangle.Width - 2, clientRectangle.Height - 2);
							if (this.rectangle_0 != null && this.rectangle_0.Length > 0)
							{
								System.Drawing.Rectangle[] array = new System.Drawing.Rectangle[this.rectangle_0.Length];
								for (int i = 0; i < this.rectangle_0.Length; i++)
								{
									array[i] = new System.Drawing.Rectangle
									{
										X = this.rectangle_0[i].X * (clientRectangle.Width - 2) / this.bitmap_0.Width + clientRectangle.X + 1,
										Y = this.rectangle_0[i].Y * (clientRectangle.Height - 2) / this.bitmap_0.Height + clientRectangle.Y + 1,
										Width = this.rectangle_0[i].Width * (clientRectangle.Width - 2) / this.bitmap_0.Width + clientRectangle.X + 1,
										Height = this.rectangle_0[i].Height * (clientRectangle.Height - 2) / this.bitmap_0.Height + clientRectangle.Y + 1
									};
								}
								System.Drawing.Pen pen2 = new System.Drawing.Pen(this.color_2, (float)this.int_0);
								graphics.DrawRectangles(pen2, array);
								pen2.Dispose();
							}
							this.bool_2 = false;
							if (this.string_1 != string.Empty)
							{
								System.Drawing.SolidBrush solidBrush = new System.Drawing.SolidBrush(this.color_0);
								System.Drawing.Font font = new System.Drawing.Font(this.Font, System.Drawing.FontStyle.Bold);
								System.Drawing.PointF point = new System.Drawing.PointF(5f, 5f);
								if (this.textOverlayLocation_0 == TextOverlayLocation.TopLeft)
								{
									point = new System.Drawing.PointF(5f, 5f);
								}
								else
								{
									if (this.textOverlayLocation_0 == TextOverlayLocation.TopRight)
									{
										point = new System.Drawing.PointF((float)(base.Width - this.string_1.Length - 5), 5f);
									}
									else
									{
										if (this.textOverlayLocation_0 == TextOverlayLocation.BottomLeft)
										{
											point = new System.Drawing.PointF(5f, (float)(base.Height - font.Height - 5));
										}
										else
										{
											if (this.textOverlayLocation_0 == TextOverlayLocation.BottomRight)
											{
												point = new System.Drawing.PointF((float)(base.Width - this.string_1.Length - 5), (float)(base.Height - font.Height - 5));
											}
										}
									}
								}
								graphics.DrawString(this.string_1, font, solidBrush, point);
								solidBrush.Dispose();
							}
						}
						else
						{
							System.Drawing.SolidBrush solidBrush2 = new System.Drawing.SolidBrush(this.ForeColor);
							graphics.DrawString((this.string_0 == null) ? "Connecting ..." : this.string_0, this.Font, solidBrush2, new System.Drawing.PointF(5f, 5f));
							solidBrush2.Dispose();
						}
					}
					pen.Dispose();
				}
			}
			catch (Exception ex)
			{
				if (this.videoSourceErrorEventHandler_0 != null)
				{
					this.videoSourceErrorEventHandler_0.Invoke(this, new VideoSourceErrorEventArgs("VideoSourcePlayer_Paint: " + ex.Message));
				}
			}
		}
		private void method_2()
		{
			lock (this.object_0)
			{
				if (this.bool_0 && this.Dock != DockStyle.Fill && base.Parent != null)
				{
					System.Drawing.Rectangle clientRectangle = base.Parent.ClientRectangle;
					int num = 320;
					int num2 = 240;
					if (this.bitmap_0 != null)
					{
						num = this.bitmap_0.Width;
						num2 = this.bitmap_0.Height;
					}
					base.SuspendLayout();
					base.Location = new System.Drawing.Point((clientRectangle.Width - num - 2) / 2, (clientRectangle.Height - num2 - 2) / 2);
					base.Size = new System.Drawing.Size(num + 2, num2 + 2);
					base.ResumeLayout();
				}
			}
		}
		private void ivideoSource_0_NewFrame(object sender, NewFrameEventArgs e)
		{
			try
			{
				if (!this.bool_3)
				{
					lock (this.object_0)
					{
						if (this.bitmap_0 != null)
						{
							this.bitmap_0.Dispose();
							this.bitmap_0 = null;
						}
						this.bitmap_0 = (System.Drawing.Bitmap)e.Frame.Clone();
						this.string_0 = null;
						this.dateTime_0 = DateTime.Now;
						if (this.newFrameHandler_0 != null)
						{
							this.newFrameHandler_0(this, ref this.bitmap_0);
						}
					}
					base.Invalidate();
				}
			}
			catch (Exception ex)
			{
				if (this.videoSourceErrorEventHandler_0 != null)
				{
					this.videoSourceErrorEventHandler_0.Invoke(this, new VideoSourceErrorEventArgs("videoSource_NewFrame: " + ex.Message));
				}
			}
		}
		private void ivideoSource_0_VideoSourceError(object sender, VideoSourceErrorEventArgs e)
		{
            this.string_0 = e.Description;
			if (this.videoSourceErrorEventHandler_0 != null)
			{
				this.videoSourceErrorEventHandler_0.Invoke(this, e);
			}
			base.Invalidate();
		}
		private void ivideoSource_0_PlayingFinished(object object_1, ReasonToFinishPlaying reasonToFinishPlaying_0)
		{
			switch (reasonToFinishPlaying_0)
			{
			case ReasonToFinishPlaying.EndOfStreamReached:
				this.string_0 = "Video has finished";
				break;
			case ReasonToFinishPlaying.StoppedByUser:
				this.string_0 = "Video was stopped";
				break;
			default:
				this.string_0 = "Video has finished for unknown reason";
				break;
			}
			base.Invalidate();
		}
		private void VideoSourcePlayer_ParentChanged(object sender, EventArgs e)
		{
			if (this.control_0 != null)
			{
				this.control_0.SizeChanged -= new EventHandler(this.control_0_SizeChanged);
			}
			this.control_0 = base.Parent;
			if (this.control_0 != null)
			{
				this.control_0.SizeChanged += new EventHandler(this.control_0_SizeChanged);
			}
		}
		private void control_0_SizeChanged(object sender, EventArgs e)
		{
			this.method_2();
		}
		private void timer_2_Elapsed(object sender, EventArgs e)
		{
			if (this.ivideoSource_0 != null)
			{
                this.int_6[this.int_4] = this.ivideoSource_0.FramesReceived;
                this.long_0[this.int_4] = (long)this.ivideoSource_0.BytesReceived;
                this.ivideoSource_0.FramesReceived = 0;
                this.ivideoSource_0.BytesReceived = 0;
				if (++this.int_4 >= 15)
				{
					this.int_4 = 0;
				}
				if (this.int_5 < 15)
				{
					this.int_5++;
				}
				this.float_0 = 0f;
				this.float_1 = 0f;
				for (int i = 0; i < this.int_5; i++)
				{
					this.float_0 += (float)this.int_6[i];
					this.float_1 += (float)this.long_0[i];
				}
				this.float_0 /= (float)this.int_5;
				this.float_1 /= (float)(this.int_5 * 1024);
				if (this.float_0 > 30f)
				{
					this.float_0 = 30f;
				}
				this.int_6[this.int_4] = 0;
				this.int_6[this.int_4] = 0;
			}
		}
		public void StartRecording()
		{
			if (this.bool_4 && this.videoFolder != string.Empty)
			{
				this.method_3();
				string text = this.videoFolder + "\\" + DateTime.Now.ToString("yyyyMMdd");
				if (!Directory.Exists(text))
				{
					Directory.CreateDirectory(text);
				}
				this.string_2 = text + "\\" + DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".avi";
				int num = this.int_2;
				if (num <= 0 || (float)num > this.float_0)
				{
					num = (int)Math.Floor((double)this.float_0);
				}
				if (num < 1)
				{
					num = 1;
				}
				this.timer_0.Interval = (double)((int)Math.Floor(1000.0 / (double)num));
				this.timer_0.Elapsed += new ElapsedEventHandler(this.timer_0_Elapsed);
				this.timer_0.Start();
				this.timer_1.Interval = 60000.0;
				this.timer_1.Elapsed += new ElapsedEventHandler(this.timer_1_Elapsed);
				this.timer_1.Start();
			}
		}
		public void StopRecording()
		{
			this.timer_0.Elapsed -= new ElapsedEventHandler(this.timer_0_Elapsed);
			this.timer_0.Stop();
			if (this.aviwriter_0 != null)
			{
				this.aviwriter_0.Dispose();
				this.aviwriter_0 = null;
				this.queue_0.Clear();
			}
			this.timer_1.Elapsed -= new ElapsedEventHandler(this.timer_1_Elapsed);
			this.timer_1.Stop();
			this.int_1 = 0;
			this.CloseRecordingWorker();
			if (this.videoWriterFinishEventHandler_0 != null && File.Exists(this.string_2))
			{
				this.videoWriterFinishEventHandler_0(this, this.string_2);
			}
		}
		private void timer_1_Elapsed(object sender, EventArgs e)
		{
			try
			{
				this.int_1++;
				if (this.int_1 >= this.int_3)
				{
					this.int_1 = 0;
					this.StopRecording();
					if (this.bool_4)
					{
						this.StartRecording();
					}
				}
			}
			catch (Exception ex)
			{
				if (this.videoSourceErrorEventHandler_0 != null)
				{
					this.videoSourceErrorEventHandler_0.Invoke(this, new VideoSourceErrorEventArgs("videoLengthTimer_Elapsed: " + ex.Message));
				}
			}
		}
		private void timer_0_Elapsed(object sender, EventArgs e)
		{
			lock (this.object_0)
			{
				if (this.bitmap_0 != null)
				{
					try
					{
						System.Drawing.Bitmap bitmap = (System.Drawing.Bitmap)this.bitmap_0.Clone();
						int width = bitmap.Width;
						int height = bitmap.Height;
						if (width > 0 && height > 0)
						{
							if (this.aviwriter_0 == null)
							{
								int num = this.int_2;
								if (num <= 0 || (float)num > this.float_0)
								{
									num = (int)Math.Floor((double)this.float_0);
								}
								if (num < 1)
								{
									num = 1;
								}
								this.timer_0.Interval = (double)((int)Math.Floor(1000.0 / (double)num));
								this.aviwriter_0 = new AVIWriter(this.string_3, num);
								this.aviwriter_0.Quality = 100;
								this.aviwriter_0.Open(this.string_2, width, height);
								this.queue_0.Clear();
							}
							if (this.aviwriter_0 != null)
							{
								this.queue_0.Enqueue(bitmap);
								if (this.backgroundWorker_0 != null && !this.backgroundWorker_0.IsBusy)
								{
									this.backgroundWorker_0.RunWorkerAsync();
								}
							}
						}
					}
					catch (Exception ex)
					{
						if (this.videoSourceErrorEventHandler_0 != null)
						{
							this.videoSourceErrorEventHandler_0.Invoke(this, new VideoSourceErrorEventArgs("addFrameTimer_Elapsed: " + ex.Message));
						}
					}
				}
			}
		}
		private void method_3()
		{
			try
			{
				this.backgroundWorker_0 = new BackgroundWorker();
				this.backgroundWorker_0.WorkerSupportsCancellation = true;
				this.backgroundWorker_0.WorkerReportsProgress = true;
				this.backgroundWorker_0.DoWork += new DoWorkEventHandler(this.backgroundWorker_0_DoWork);
				this.backgroundWorker_0.RunWorkerCompleted += new RunWorkerCompletedEventHandler(this.backgroundWorker_0_RunWorkerCompleted);
				this.backgroundWorker_0.ProgressChanged += new ProgressChangedEventHandler(this.backgroundWorker_0_ProgressChanged);
			}
			catch (Exception ex)
			{
				if (this.videoSourceErrorEventHandler_0 != null)
				{
					this.videoSourceErrorEventHandler_0.Invoke(this, new VideoSourceErrorEventArgs("Error while initial recording worker: " + ex.Message));
				}
			}
		}
		private void backgroundWorker_0_DoWork(object sender, DoWorkEventArgs e)
		{
			BackgroundWorker backgroundWorker = sender as BackgroundWorker;
			if (!backgroundWorker.CancellationPending)
			{
				try
				{
					if (this.aviwriter_0 != null)
					{
						int width = this.aviwriter_0.Width;
						int height = this.aviwriter_0.Height;
						if (width > 0 && height > 0)
						{
							while (this.queue_0.Count > 0)
							{
								System.Drawing.Bitmap bitmap = (System.Drawing.Bitmap)this.queue_0.Dequeue();
								if (bitmap.Width != width)
								{
									goto IL_79;
								}
								if (bitmap.Height != height)
								{
									goto IL_79;
								}
								System.Drawing.Bitmap bitmap2 = (System.Drawing.Bitmap)bitmap.Clone();
								IL_88:
								if (bitmap2 != null)
								{
									this.aviwriter_0.AddFrame(bitmap2);
									bitmap2.Dispose();
								}
								bitmap.Dispose();
								Thread.Sleep(1);
								continue;
								IL_79:
								bitmap2 = new System.Drawing.Bitmap(bitmap, new System.Drawing.Size(width, height));
								goto IL_88;
							}
						}
					}
					return;
				}
				catch (Exception ex)
				{
					this.StopRecording();
					if (this.videoSourceErrorEventHandler_0 != null)
					{
						this.videoSourceErrorEventHandler_0.Invoke(this, new VideoSourceErrorEventArgs("Error in recording worker Do_Work: " + ex.Message));
					}
					if (this.bool_4)
					{
						this.StartRecording();
					}
					return;
				}
			}
			this.StopRecording();
		}
		private void backgroundWorker_0_ProgressChanged(object sender, ProgressChangedEventArgs e)
		{
		}
		private void backgroundWorker_0_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
		{
		}
		public void CloseRecordingWorker()
		{
			if (this.backgroundWorker_0 != null)
			{
				this.backgroundWorker_0.DoWork -= new DoWorkEventHandler(this.backgroundWorker_0_DoWork);
				this.backgroundWorker_0.RunWorkerCompleted -= new RunWorkerCompletedEventHandler(this.backgroundWorker_0_RunWorkerCompleted);
				this.backgroundWorker_0.ProgressChanged -= new ProgressChangedEventHandler(this.backgroundWorker_0_ProgressChanged);
				this.backgroundWorker_0.CancelAsync();
				this.backgroundWorker_0 = null;
			}
		}
	}
}
